﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zeus.Biz.DAL.EF.Northwind;
using Zeus.Biz.Northwind.Model;
using Zeus.Biz.Northwind.Model.ViewModel;
using Zeus.Extension.Web;

namespace Zeus.Biz.Northwind.BLL
{
    public class EmployeePage
    {
        private int pageIndex;
        private int pageSize;

        public EmployeePage(int pageIndex, int pageSize)
        {
            this.pageIndex = pageIndex;
            this.pageSize = pageSize;
        }

        public EmployeeViewModel GetRecordSet()
        {
            using (var context = new NorthwindContext("NorthwindConnection"))
            {
                var employees = from entry in context.Employees
                                orderby entry.EmployeeID
                                select entry;

                IList<Employee> empList = employees.ToList();

                PagedList<Employee> empPageList = empList.ToPagedList(pageIndex, pageSize);

                EmployeeViewModel employeeInfo = new EmployeeViewModel();
                employeeInfo.EmployeePageList = empPageList;
                employeeInfo.TotalRecordNum = empPageList.TotalCount;
                if (empPageList.TotalCount % empPageList.PageSize == 0)
                {
                    employeeInfo.TotalPageNum = empPageList.TotalCount / empPageList.PageSize;
                }
                else
                {
                    employeeInfo.TotalPageNum = empPageList.TotalCount / empPageList.PageSize + 1;
                }
                employeeInfo.PageSize = empPageList.PageSize;

                return employeeInfo;
            }
        }
    }
}
